package com.mobi.Survey.lib.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * DBHelper.java Creation of Tables in database
 * 
 */
public class DBHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "db_survey.db";
	private static final int DATABASE_VERSION = 3;

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		TableFactory.getFactory().createTable(new SurveyDetailTable(), db);
		TableFactory.getFactory().createTable(new SurveyResultTable(), db);
		TableFactory.getFactory().createTable(new SurveyTable(), db);
		TableFactory.getFactory().createTable(new SubscriptionTable(), db);
		TableFactory.getFactory().createTable(new AggregateResultTable(), db);
		TableFactory.getFactory().createTable(new AddressTable(), db);
		TableFactory.getFactory().createTable(new AlarmTable(), db);
		TableFactory.getFactory().createTable(new FavoriteTable(), db);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		 

		String sql = null;
		if (oldVersion == 1) {

			// Detail Table Rows
			sql = "ALTER TABLE " + SurveyDetailTable.TABLE + " ADD "
					+ SurveyDetailTable.COL_CTRL_WIDTH + " integer DEFAULT 0 ";
			db.execSQL(sql);
			sql = "ALTER TABLE " + SurveyDetailTable.TABLE + " ADD "
					+ SurveyDetailTable.COL_CTRL_HEIGHT + " integer DEFAULT 0 ";
			db.execSQL(sql);

			// SurveyTable Rows

			sql = "ALTER TABLE " + SurveyTable.TABLE + " ADD "
					+ SurveyTable.COL_BG_COLOR + " text DEFAULT null ";
			db.execSQL(sql);

			sql = "ALTER TABLE " + SurveyTable.TABLE + " ADD "
					+ SurveyTable.COL_BG_IMAGE_URL + " text DEFAULT null ";
			db.execSQL(sql);

			sql = "ALTER TABLE " + SurveyTable.TABLE + " ADD "
					+ SurveyTable.COL_FONT_COLOR + " text DEFAULT null ";
			db.execSQL(sql);
			
			
			 

		}else if(oldVersion == 2){

			sql = "ALTER TABLE " + SurveyTable.TABLE + " ADD "
					+ SurveyTable.COL_TIMER_BTN_VAL + " text DEFAULT null ";
			db.execSQL(sql);
			
		}

	}

}
